<?php

require_once(WSB_MODULES_PATH . 'cms/Url.php');

class Cms_PlaceholderController extends WSB_Controller_Action
{
	public function init()
	{
		parent::init(); // immer zuerst!
	    $this->_models = array('Cms_Model_DbTable_Placeholder');

		$navigation = $this->_helper->getHelper('Navigation');
		$navigation->add(Cms_Url::placeholderListUrl(), '&Uuml;bersicht');
		$navigation->add(Cms_Url::placeholderDeleteUnusedUrl(), 'L&ouml;sche unbenutzte Platzhalter');

	}

	public function fckeditorAction()
	{
		echo $this->view->render('index/fckeditor.phtml');
		exit;
	}

	public function deleteunusedAction()
	{

        // Benutzte Platzhalter-IDs
	    $usedIds = array();

	    // Hier werden alle Platzhalter ausgelesen, die in "html" der
	    // Tabelle template gespeichert sind. Unabhaengig davon, ob
	    // diese referenziert sind oder nicht.
	    $table = new Cms_Model_DbTable_Template();
	    $where = $table->getAdapter()->quoteInto('deleted=?', 0);
        foreach($table->fetchAll($where) AS $template) {
            foreach($template->getPlaceholders() AS $p) {
                $usedIds[$p->id] = $p->id;
            }
        }

        // In $unusedRowset sind alle "ungenutzen" Platzhalter gespeichert.
        if(count($usedIds) > 0) {
        	$table = new Cms_Model_DbTable_Placeholder();
            $unusedRowset = $table->fetchAll('deleted=0 AND id NOT IN (' . implode(',', $usedIds) . ')');
        }
	    if($this->_getParam('confirmed') == 'yes') {
            foreach($unusedRowset AS $p) {
                $p->delete();
                WSB_Log::info(sprintf('Deleted placeholder (id=%s)', $p->id));
            }
            $this->view->unusedPlaceholders = array();
            $this->_helper->FlashMessenger->addMessage('Alle unbenutzten Platzhalter wurden gelöscht.');
            $this->_helper->Redirect(Cms_Url::placeholderListUrl());

	    } else {

			if(count($unusedRowset) == 0) {
				$this->render('no-unused');
			} else {

	            $this->view->unusedPlaceholders = $unusedRowset;

		        // Settings fuer htmlTable->Viewhelper
				$this->view->columns = array(
					array('key' => 'id', 'label' => 'ID', 'sortable' => 'true'),
					array('key' => 'name', 'label' => 'Name', 'sortable' => 'true'),
					array('key' => 'description', 'label' => 'Beschreibung', 'sortable' => 'true'),
					array('key' => 'created', 'label' => 'Erstellt', 'sortable' => 'true'),
					array('key' => 'modified', 'label' => 'Modifiziert', 'sortable' => 'true'),
				);
				$this->view->fields = array(
					array('key' => 'id'),
					array('key' => 'name'),
					array('key' => 'description'),
					array('key' => 'created'),
					array('key' => 'modified')
				);
				$this->view->settings = array('sortedBy' => array(
					'key' => 'name', 'dir' => 'asc')
				);

        		$this->render('deleteunused');

			}

	    }

	}

	public function ajaxAddAction()
	{
		$id = $this->_request->getPost('id');
	    $name = $this->_request->getPost('name');
	    $description = $this->_request->getPost('description');
	    $table = new Cms_Model_DbTable_Placeholder();
        $item = $table->createRow();
        if(!empty($name)) {
            if((int)$id > 0) {
	            $item->id = $id;
            }
            $item->name = $name;
            $item->description = $description;
            $item->save();
        }
        echo Zend_Json_Encoder::encode($item->toArray());
        exit;
	}

	public function ajaxGetAction()
	{
		$id = intval($this->_getParam('id', 0));
		if($id > 0) {
	    	$table = new Cms_Model_DbTable_Placeholder();
			$select = $table->select();
			$select->where('id=?', $id);
	        $row = $table->fetchRow($select);
	        if($row instanceof Cms_Model_Placeholder) {
	        	echo Zend_Json_Encoder::encode($row->toArray());
	        }
		}
        exit;
	}

}
